package com.ruoyi.system.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TBOrder;
import com.ruoyi.system.mapper.TBOrderMapper;
import com.ruoyi.system.service.ITBOrderDeviceService;
import com.ruoyi.system.service.ITBOrderService;
import com.ruoyi.system.service.ITBOrderSpreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author pspyy
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/system/order")
@Api("订单管理")
public class TBOrderController extends BaseController
{
    @Autowired
    private ITBOrderService tBOrderService;

    @Autowired
    private ITBOrderDeviceService tBOrderDeviceService;

    @Autowired
    private ITBOrderSpreadService tBOrderSpreadService;

    @Autowired
    private TBOrderMapper tBOrderMapper;
    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    @ApiOperation("查询订单列表")
    public TableDataInfo list(TBOrder tBOrder) {
        startPage();
        List<TBOrder> list = tBOrderMapper.selectTBOrderList2(tBOrder);
        PageHelper.clearPage();
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出订单列表")
    public void export(HttpServletResponse response, TBOrder tBOrder)
    {
        List<TBOrder> list = tBOrderService.selectTBOrderList(tBOrder);
        for (TBOrder order:list) {
            Long aLong = tBOrderDeviceService.selectByOrderNum(order.getOrderNum());
            order.setOrderDeviceNum(aLong);
            List<Long> orderSpreadIdList = tBOrderSpreadService.selectOrderSpreadIdList(order.getOrderNum());
            if(orderSpreadIdList.size() > 0){
                List<Long> longs = tBOrderSpreadService.selectOrderSpreadStatue(order.getOrderNum());
                if(longs.size() > 0){
                    order.setOrderSpreadNum(longs.size() + "/" + orderSpreadIdList.size());
                }else{
                    order.setOrderSpreadNum("0" + "/" + orderSpreadIdList.size());
                }
            }else{
                order.setOrderSpreadNum("0");
            }
        }
        ExcelUtil<TBOrder> util = new ExcelUtil<TBOrder>(TBOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取订单详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBOrderService.selectTBOrderById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增订单")
    public AjaxResult add(@RequestBody TBOrder tBOrder)
    {
        int i = tBOrderService.checkOrderNumUnique(tBOrder.getOrderNum());
        if(i > 0){
            return AjaxResult.error("新增订单'" + tBOrder.getOrderNum() + "'失败，订单已存在");
        }
        return toAjax(tBOrderService.insertTBOrder(tBOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改订单")
    public AjaxResult edit(@RequestBody TBOrder tBOrder)
    {
        return toAjax(tBOrderService.updateTBOrder(tBOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除订单")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tBOrderService.deleteTBOrderByIds(ids));
    }
}
